/*
 * @Author: Crism
 * @Date: 2018-08-02 11:47:24
 * @LastEditors: Crism
 * @LastEditTime: 2018-08-03 16:19:36
 * @Description: 
 * @Email: crism@qq.com
 */

const RP = require('request-promise');

exports.httpGet = httpGet;

async function httpGet(apiUrl, cnt = 0) {

    let res = null;
    let flag = true;

    await RP.get(apiUrl)
        .then((body) => {
            res = body;
        }).catch((err) => {
            flag = false;
            res = err;
        })

    if (!flag && cnt < 1) {
        //请求失败后会再发送请求,最多请求1次
        cnt++;
        let err_msg = '请求失败' + cnt + '次，url:' + apiUrl + ',err:' + res;
        console.log(err_msg);
        // global.ErrorLog.write(err_msg);
        await sleep(500);
        res = await httpGet(apiUrl, cnt);
    }
    return res;
}